package snafu.implementation.actions.drop;

import java.util.List;

import snafu.framework.actions.AbstractAction;
import snafu.framework.actions.Intention;
import snafu.framework.objects.WorldObject;
import snafu.framework.states.StateItself;
import snafu.framework.view.Access;
import snafu.framework.worlds.Position;
import snafu.framework.worlds.WorldMap;
import snafu.implementation.states.literals.have.HaveLiteral;
import snafu.implementation.states.literals.position.PositionLiteral;
import snafu.implementation.states.literals.position.PositionLiteralItself;

public class DropAction extends AbstractAction {

	public DropAction(List<WorldObject> targets, Intention<?> intention) {
		super(targets, intention);
	}

	@Override
	public void execute(WorldMap worldMap) {
		// get the only target
		WorldObject target = targets.get(0);
		
		// get state of the agent
		StateItself agentState = (StateItself) agent.getState();
		StateItself targetState = (StateItself) target.getState();
		
		// get HaveLteral for target
		HaveLiteral haveLiteral = null;
		List<HaveLiteral> literals = agentState.getLiterals(HaveLiteral.class);
		for (HaveLiteral literal : literals) {
			if (literal.getValue().equals(target)) {
				haveLiteral = literal;
				break;
			}
		}
		
		// get target position
		Position position = agentState.getLiteral(PositionLiteral.class);
		// add target to map
		worldMap.addObject(position, target);
		// remove target from agent
		agentState.removeLiteral(haveLiteral);
		// set current target position
		targetState.setLiteral(new PositionLiteralItself(position, Access.PRIVATE));
	}

	@Override
	public String output() {
		return agent + " dropped " + targets.get(0);
	}

	@Override
	public boolean supportMultiTargets() {
		return false;
	}

}
